/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import javax.persistence.EntityManager;
import supermarket.*;
import model.*;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author koag
 */
public class RelateProductsStoresPanel extends javax.swing.JPanel {
    // Δήλωσε τις μεταβλητές

    private DBmanager db;
    private EntityManager em;
    
    private List<Store> allStores;
    
    private List<Product> AvailableProducts;
    private DefaultListModel modelP;
   
    private List<Product> StoreProducts;
    private DefaultListModel modelS;
    
    private Store SelectedStore;
    private int comboIndex = 0;
    private boolean UpdatesPending;

    /**
     * Creates new form RelateProductsStoresPanel
     */
    public RelateProductsStoresPanel(DBmanager db) {
        this.db = db;
        this.em = db.getEm();
        initComponents();
        updateStoresCombo();
    }

    private void setAvailableProductList() {

        AvailableProducts = this.db.getProductList();

        if (this.AvailableProducts.isEmpty()) {
            return;
        }
        //Αφαιρώ τα στοιχεία στο AvailableProducts που υπάρχουν και 
        // στην λίστα του καταστήματος.
        for (int i = 0; i < StoreProducts.size(); i++) {
            Product p = (Product) StoreProducts.get(i);
            for (int j = 0; j < AvailableProducts.size(); j++) {
                Product avail = (Product) AvailableProducts.get(j);
                if (avail.equals(p)) {
                    AvailableProducts.remove(j);
                }
            }
        }
        // Τα προιόντα που μείναν δεν τα έχει το κατάστημα
        modelP = new DefaultListModel();
        RemainingProductsList.setModel(modelP);
        // Καθάρισε Την λίστα 
        modelP.removeAllElements();
        //Βάζουμε τα στοιχεία στο model άρα και στην λίστα.
        for (int i = 0; i < AvailableProducts.size(); i++) {
            Product p = (Product) AvailableProducts.get(i);
            modelP.addElement(p.getName());
        }
    }

    private void setStoreProducts() {
        
        StoreProducts = this.SelectedStore.getProductList();
        // Δημιούργησε DefaultListModel και συσχετισέτο με την λίστα μας
        modelS = new DefaultListModel();
        StoreProductList.setModel(modelS);
        // Καθάρισε Την λίστα 
        modelS.removeAllElements();

        //Βάζουμε τα στοιχεία στο model άρα και στην λίστα.
        for (int i = 0; i < StoreProducts.size(); i++) {
            Product p = (Product) StoreProducts.get(i);
            modelS.addElement(p.getName());
        }
    }

    private void updateStoresCombo() {
        this.UpdatesPending = false;
        // Καθάρισε την λίστα και το Combo
        this.comboIndex = 0;
        this.combo.removeAllItems();

        // Ζήτα την λίστα με τα καταστήματα απο την ΒΔ.
        this.allStores = db.getStoreList();
        //Βάζουμε τα στοιχεία στο Combo 
        for (int i = 0; i < allStores.size(); i++) {
            this.combo.addItem(allStores.get(i).getName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        RemainingProductsList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        StoreProductList = new javax.swing.JList();
        b_appendToStore = new javax.swing.JButton();
        b_appendToAvailable = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        b_cancel = new javax.swing.JButton();
        b_Save = new javax.swing.JButton();
        combo = new javax.swing.JComboBox();

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText("Προιόντα και Καταστήματα");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Επέλεξε Κατάστημα ");

        jScrollPane1.setViewportView(RemainingProductsList);

        StoreProductList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        StoreProductList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                StoreProductListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(StoreProductList);

        b_appendToStore.setText("------>");
        b_appendToStore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_appendToStoreActionPerformed(evt);
            }
        });

        b_appendToAvailable.setText("<------");
        b_appendToAvailable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_appendToAvailableActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Διαθέσιμα Προιόντα");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Προιόντα Καραστήματος");

        b_cancel.setText("Ακύρωση");
        b_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_cancelActionPerformed(evt);
            }
        });

        b_Save.setText("Αποθήκευση");
        b_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_SaveActionPerformed(evt);
            }
        });

        combo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                comboMousePressed(evt);
            }
        });
        combo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(combo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(b_appendToStore, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                            .addComponent(b_appendToAvailable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(b_Save, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(b_cancel, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(b_appendToStore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(b_appendToAvailable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(101, 101, 101)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(b_Save)
                    .addComponent(b_cancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void b_appendToStoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_appendToStoreActionPerformed
        // Αφαίρεσε απο υπόλοιπα προιόντα
        int availIndex = RemainingProductsList.getSelectedIndex();
        if (availIndex == -1){return;}
        
        Product p = (Product) AvailableProducts.get(availIndex);
        AvailableProducts.remove(availIndex);
        // Ενημέρωσε και την λίστα
        modelP.remove(availIndex);

        // Πρόσθεσε στα προιόντα του καταστήματος 
        StoreProducts.add(p);
        p.getStoreList().add(SelectedStore);
        // Ενημέρωσε και την λίστα
        modelS.addElement(p.getName());
        this.UpdatesPending = true;
    }//GEN-LAST:event_b_appendToStoreActionPerformed

    private void comboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboActionPerformed
        // An υπάρχουν αλαγές ρώτα τον χρήστη αν θέλει να τις σώσει  
        if (this.UpdatesPending) {
            // Επιβεβαίωση Καταστροφής αλαγών
            int choice = JOptionPane.showConfirmDialog(this, "Εχετε κάνει αλαγές που δεν έχουν σωθεί"
                    + "\n Θελετε να τις Σώσετε;",
                    "Επιβεβαίωση Ενημέρωσης καταστήματος",
                    JOptionPane.YES_NO_OPTION);
            if (choice != 0) {
                return;
            }
        }
        this.UpdatesPending = false;

        // Ο χρήστης άλαξε επιλεγμένο κατάστημα
        // Ενημέρωσε το comboIndex και κάνε update
        // τις 2 λίστες με τα προιόντα
        this.comboIndex = combo.getSelectedIndex();
        if (this.comboIndex == -1) {
            return;
        }
        
        this.SelectedStore = (Store) this.allStores.get(this.comboIndex);
        this.setStoreProducts();
        this.setAvailableProductList();
        this.UpdatesPending = false;
    }//GEN-LAST:event_comboActionPerformed

    private void StoreProductListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StoreProductListMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_StoreProductListMouseClicked

    private void comboMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboMousePressed
    }//GEN-LAST:event_comboMousePressed

    private void b_appendToAvailableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_appendToAvailableActionPerformed
        // Αφαίρεσε απο υπόλοιπα προιόντα του καταστήματος
        int availIndex = StoreProductList.getSelectedIndex();
        if (availIndex == -1){return;}
        
        Product p = (Product) StoreProducts.get(availIndex);
        StoreProducts.remove(availIndex);
        p.getStoreList().remove(this.SelectedStore);
        modelS.remove(availIndex);

        // Πρόσθεσε στα υπόλοιπα προιόντα 
        AvailableProducts.add(p);
        modelP.addElement(p.getName());

        this.UpdatesPending = true;
    }//GEN-LAST:event_b_appendToAvailableActionPerformed

    private void b_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_SaveActionPerformed

        this.SelectedStore.setProductList(this.StoreProducts);
        int result = db.updateStore(SelectedStore);
        // Ελεγξε αν ήταν επιτυχής η ενημέρωση
        // και ενημέρωσε χρήστη
        if (result == 1) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
            this.UpdatesPending = false;
        } else {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_b_SaveActionPerformed

    private void b_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_cancelActionPerformed
       // Αν δεν έχουν γίνει αλλαγές μην κάνεις τίποτα
        if (!this.UpdatesPending){return;}
        
        // Επιβεβαίωση ακύρωσης αλλαγών
        int choice = JOptionPane.showConfirmDialog(this, "Ακύρωση όλων των αλλαγών;",
            "Ακύρωση αλλαγών",
            JOptionPane.YES_NO_OPTION);

        if (choice != 0)
        return;
        
        this.UpdatesPending = false;
        
        // Σβήσε όλες τις αλλαγές στο κατάστημα
          this.db.abandonChanges(SelectedStore);

        // Εμφάνισε εκ νέου τις πληροφορίες για το επιλεγμένο κατάστημα
           comboActionPerformed(null);
    }//GEN-LAST:event_b_cancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList RemainingProductsList;
    private javax.swing.JList StoreProductList;
    private javax.swing.JButton b_Save;
    private javax.swing.JButton b_appendToAvailable;
    private javax.swing.JButton b_appendToStore;
    private javax.swing.JButton b_cancel;
    private javax.swing.JComboBox combo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
